package cn.liberals.org.service.impl;

import cn.liberals.basic.service.impl.BaseServiceImpl;
import cn.liberals.basic.utils.MD5Utils;
import cn.liberals.basic.utils.StrUtils;
import cn.liberals.org.domain.Employee;
import cn.liberals.org.mapper.EmployeeMapper;
import cn.liberals.org.service.IEmployeeService;
import cn.liberals.user.domain.Logininfo;
import cn.liberals.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    /* 
    * @Title: save
    * @Description: 同步保存的sava
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/7 0:45  
    * @Parameters: 
    * @Return 
    */
    public void save(Employee employee) {
        // 初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.save(employee);
        // 初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 将logininfo的主键Id设置到employee的外键，并修改
        employee.setShopId(logininfo.getId());
        // 初始化关联数据
        initRelationTable(employee);
    }


    /* 
    * @Title: initRelationTable
    * @Description: 初始化关联数据
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/13 18:37  
    * @Parameters: 
    * @Return 
    */
    public void initRelationTable(Employee employee){
        // 2.根据roleId和菜单Id和权限Id进行新增
        if (employee.getRoles() != null){
            // 1.根据roleId删除角色菜单和角色权限关联表对应数据
            employeeMapper.removeEmployeeRole(employee.getId());
            employeeMapper.saveEmployeeRole(employee.getId(), employee.getRoles());
        }
    }

    /* 
    * @Title: initEmployee
    * @Description: 初始化employee
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/7 0:08  
    * @Parameters: 
    * @Return 
    */
    public void initEmployee(Employee employee) {
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        // 根据盐值进行加密
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }

    /* 
    * @Title: employee2Logininfo
    * @Description: 将employee的数据拷贝到logininfo中
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/7 0:09  
    * @Parameters: 
    * @Return 
    */
    private Logininfo employee2Logininfo(Employee employee) {
        // 拷贝数据
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        // 设置用户类型为员工
        logininfo.setType(0);
        // 返还logininfo对象
        return logininfo;
    }
    
    @Override
    public void remove(Long id) {
        employeeMapper.removeEmpRole(id);
        // 查到对应的logininfo表的id
        Long logininfoId = employeeMapper.loadById(id).getLogininfoId();
        employeeMapper.remove(id);
        if (logininfoId!=null){
            logininfoMapper.remove(logininfoId);
        }

    }

    @Override
    public void update(Employee employee) {
        // 初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.update(employee);
        // 初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.update(logininfo);
        // 将logininfo的主键Id设置到employee的外键，并修改
        employee.setShopId(logininfo.getId());
        // 初始化关联数据
        initRelationTable(employee);
    }


    @Override
    public Employee loadByLogininfoId(Long logininfoId) {
        return employeeMapper.loadByLoginInfoId(logininfoId);
    }

    @Override
    public List<Employee> loadByShopId(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}
